﻿using MemoDb.Tests.Model;
using NUnit.Framework;

namespace MemoDb.Tests
{
    [TestFixture]
    public class UpdateTests
    {
        [Test]
        public void ValuesOnly()
        {
            var memo = new Memo()
                            .Map<City>();

            var buenosAires = new City
            {
                Id = 1,
                Name = "Buenos Aires"
            };
            using (var s = memo.CreateSession())
            {
                s.Insert(buenosAires);
                s.Flush();
            }

            City buenosAires2;

            using (var s = memo.CreateSession())
            {
                var test = s.GetById<City>(buenosAires.Id);
                test.Name = "Bs As";
                s.Flush();
            }

            using (var s = memo.CreateSession())
            {
                buenosAires2 = s.GetById<City>(buenosAires.Id);
            }

            Assert.AreEqual("Bs As", buenosAires2.Name);
        }

        [Test]
        public void ValuesAndReferences()
        {
            var memo = new Memo()
                            .Map<City>()
                            .Map<Country>();
            var argentina = new Country
            {
                Id = 1,
                Name = "Argentina"
            };
            
            var other = new Country
            {
                Id = 2,
                Name = "Other"
            };

            var buenosAires = new City
            {
                Id = 1,
                Name = "Buenos Aires",
                Country = argentina
            };

            using (var s = memo.CreateSession())
            {
                s.Insert(argentina);
                s.Insert(buenosAires);
                s.Insert(other);
                s.Flush();
            }

            using (var s = memo.CreateSession())
            {
                var test = s.GetById<City>(buenosAires.Id);
                var country = s.GetById<Country>(other.Id);
                test.Country = country;
                s.Flush();
            }
            
            City buenosAires2;
            Country other2;
            using (var s = memo.CreateSession())
            {
                buenosAires2 = s.GetById<City>(buenosAires.Id);
                other2 = s.GetById<Country>(other.Id);
            }

            Assert.AreEqual(other2, buenosAires2.Country);
        }

        [Test]
        public void ChangesOutsideASessionShouldNotBeStored()
        {
            var memo = new Memo()
                            .Map<City>();

            var buenosAires = new City
            {
                Id = 1,
                Name = "Buenos Aires"
            };
            
            using (var s = memo.CreateSession())
            {
                s.Insert(buenosAires);
                s.Flush();
            }

            buenosAires.Name = "bla";

            City buenosAires2;

            using (var s = memo.CreateSession())
            {
                buenosAires2 = s.GetById<City>(buenosAires.Id);
            }

            Assert.AreEqual("Buenos Aires", buenosAires2.Name);
        }

        [Test]
        public void ChangesWithoutFlushShouldNotBeStored()
        {
            var memo = new Memo()
                            .Map<City>();

            var buenosAires = new City
            {
                Id = 1,
                Name = "Buenos Aires"
            };
            using (var s = memo.CreateSession())
            {
                s.Insert(buenosAires);
                s.Flush();
            }

            City buenosAires2;

            using (var s = memo.CreateSession())
            {
                var test = s.GetById<City>(buenosAires.Id);
                test.Name = "Bs As"; //Not flushed!
            }

            using (var s = memo.CreateSession())
            {
                buenosAires2 = s.GetById<City>(buenosAires.Id);
            }

            Assert.AreEqual("Buenos Aires", buenosAires2.Name);
        }

    }
}
